package foo.websystem.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import foo.base.BaseJdbcDAO;
import foo.websystem.vo.SessionUser;

@Repository
public class UserDAO extends BaseJdbcDAO {
	public List<SessionUser> getAllUser(){
		String  sql ="select  user_id, name user_name, password from sys_user order by user_id";
		List<SessionUser> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(SessionUser.class));
		return list;
	}

	public SessionUser getUser(String userId) {
		String sql = "SELECT user_id as user_id, name as user_name, password FROM sys_user WHERE user_id =?";
		// String sql = "SELECT user_id, user_name, password " + " FROM worker"
		// + " WHERE user_id = '" + userId + "'";
		List<SessionUser> list = jdbcTemplate.query(sql,
		/*
		 * 另外一種用法 new RowMapper<SessionUser>() { public SessionUser
		 * mapRow(ResultSet rs, int rowNum) throws SQLException { SessionUser su
		 * = new SessionUser(); su.setUserId(rs.getString("user_id"));
		 * su.setPassword(rs.getString("password"));
		 * su.setUserName(rs.getString("user_name")); return su; } }
		 */
		BeanPropertyRowMapper.newInstance(SessionUser.class), userId);
		logger.debug("取得使用者資料:" + list);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}

	}
}
